<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${packageName}.${className_x}.mapper.${className_d}Mapper">
	<resultMap id="BaseResultMap" type="${packageName}.${className_x}.entity.${className_d}Vo">
		<#list tableCarrays as tableCarray>
		   <result column="${tableCarray.carrayName}" property="${tableCarray.carrayName_x}" />
		</#list>
	</resultMap>

	<sql id="baseColumn">
		<#list tableCarrays as tableCarray>
			<#if tableCarray_has_next>
        ${tableCarray.carrayName},
	        <#else>
        ${tableCarray.carrayName}
			</#if>
		</#list>
	</sql>

    <sql id="whereCondition">
        <where>
		<#list tableCarrays as tableCarray>
            <if test=" ${tableCarray.carrayName_x} != null ">
                AND ${tableCarray.carrayName} = ${r'#'}{${tableCarray.carrayName_x}}
            </if>
		</#list>
        </where>
	</sql>

    <select id="count" parameterType="${packageName}.${className_x}.entity.${className_d}Param" resultType="java.lang.Long">
        SELECT count(*)
        FROM ${className}
        <include refid="whereCondition"/>
    </select>

    <select id="query" parameterType="${packageName}.${className_x}.entity.${className_d}Param" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumn"/>
        FROM ${className}
        <include refid="whereCondition"/>
        limit 1
    </select>

    <select id="queryByIds" parameterType="java.util.List" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumn"/>
        FROM ${className}
        WHERE id in
        <foreach collection="list" item="id" index="index" separator="," open="(" close=")">
            ${r'#'}{id}
        </foreach>
    </select>

    <select id="queryList" parameterType="${packageName}.${className_x}.entity.${className_d}Param" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumn"/>
        FROM ${className}
        <include refid="whereCondition"/>
        order by create_time desc
    </select>

    <insert id="insert" parameterType="${packageName}.${className_x}.entity.${className_d}">
        insert into ${className}
        (<include refid="baseColumn"/>)
        values
        (<#list tableCarrays as tableCarray><#if tableCarray_has_next> ${r'#'}{${tableCarray.carrayName_x}},<#else> ${r'#'}{${tableCarray.carrayName_x}} </#if></#list>)
    </insert>

    <insert id="batchInsert" parameterType="java.util.List">
        insert into ${className}
        (<include refid="baseColumn"/>)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (<#list tableCarrays as tableCarray><#if tableCarray_has_next> ${r'#'}{item.${tableCarray.carrayName_x}},<#else> ${r'#'}{item.${tableCarray.carrayName_x}} </#if></#list>)
        </foreach>
    </insert>

    <update id="updateById" parameterType="${packageName}.${className_x}.entity.${className_d}">
        update ${className}
        <set>
        <#list tableCarrays as tableCarray>
            <#if (tableCarray.carrayName_x != "id")>
            <if test=" ${tableCarray.carrayName_x} != null ">
                ${tableCarray.carrayName} = ${r'#'}{${tableCarray.carrayName_x}},
            </if>
            </#if>
        </#list>
        </set>
        where id=${r'#'}{id}
    </update>

    <sql id="deleteWhereCondition">
        <where>
            <#list tableCarrays as tableCarray>
                <#if (tableCarray.carrayName_x != "updateTime" && tableCarray.carrayName_x != "updateUser"
                && tableCarray.carrayName_x != "createTime" && tableCarray.carrayName_x != "createUser")>
                    <if test=" ${tableCarray.carrayName_x} != null ">
                        AND ${tableCarray.carrayName} = ${r'#'}{${tableCarray.carrayName_x}}
                    </if>
                </#if>
            </#list>
        </where>
    </sql>

    <update id="delete" parameterType="${packageName}.${className_x}.entity.${className_d}">
        update ${className} set deleted = 1
        <set>
            <#list tableCarrays as tableCarray>
                <#if (tableCarray.carrayName_x == "updateTime" || tableCarray.carrayName_x == "updateUser")>
            <if test=" ${tableCarray.carrayName_x} != null ">
                ${tableCarray.carrayName} = ${r'#'}{${tableCarray.carrayName_x}},
            </if>
                </#if>
            </#list>
        </set>
        <include refid="whereCondition"/>
    </update>

    <delete id="deleteRecord" parameterType="${packageName}.${className_x}.entity.${className_d}">
        delete from ${className}
        <include refid="whereCondition"/>
    </delete>

</mapper>